package com.isoft.controller;


import com.isoft.common.Result;
import com.isoft.entity.Device;
import com.isoft.mapper.DeviceMapper;
import com.isoft.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/devices")
public class DeviceController {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/add")
    public Result<?> addDevice(@RequestBody Device device) {
        Device existingDevice = deviceMapper.haveDevice(device);
        if(existingDevice != null){
            return Result.error("1","设备添加失败！");
        }
        deviceMapper.insert(device);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result<?> deleteDevice(@RequestParam("sno") String sno){
        Device existingDevice = deviceMapper.select(sno);
        if(existingDevice == null){
            return Result.error("1","设备删除失败！");
        }
        deviceMapper.delete(existingDevice);
        return Result.success();
    }
    @PutMapping("/update")
    public Result<?> updateDevice(@RequestBody Device device){
        Device existingDevice = deviceMapper.haveDevice(device);
        if(existingDevice != null){
            deviceMapper.update(device);
            return Result.success();
        }
        return Result.error("1","设备修改失败！");
    }

    @GetMapping("/select")
    public Result<?> selectDevice(@RequestParam("sno") String sno){
        Device existingDevice = deviceMapper.select(sno);
        if(existingDevice != null){
            return Result.successone(existingDevice);
        }
        return Result.error("1","设备查询失败！");
    }

    @GetMapping("/selectAll")
    public List<Device> selectAll() {
        return deviceMapper.getAllDevices();
    }
}
